/**
 * 版权所有 Copyright@@2016-03-18 1:39:12
 */
package com.fairy.sso.controller;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fairy.core.util.MD5Util;
import com.fairy.sso.SSOConstants;
import com.fairy.sso.Ticket;
import com.fairy.sso.annotation.FairyControllerExceptionLog;
import com.fairy.sso.cache.SSOCache;
import com.fairy.sso.config.FairyConfig;
import com.fairy.sso.core.SSOHelp;
import com.fairy.sso.entity.SSOUserEntity;
import com.fairy.sso.server.Factory;
import com.fairy.sso.server.SSOCacheFactory;
import com.fairy.sso.service.SSOService;
import com.fairy.web.common.FairyConstants;

/**
 * [TicketController.java.java]
 *
 * @Author: [yyk1504@163.com]
 * @CreateDate: [2016-03-18 1:39:12]
 * @Update: [说明本次修改内容] by [yyk1504@163.com] [2016-03-18 1:39:12]
 * @Version: [v1.0]
 */

@Controller
public class TicketController {

	private static Logger log = Logger.getLogger(TicketController.class);
	@Autowired(required=true)
	private FairyConfig fairyConfig;
	
	@Autowired(required=true)
	private SSOService<SSOUserEntity> ssoService;
	
	@RequestMapping ("/ticket.do")
	@FairyControllerExceptionLog(description="获取ticket")
	public ModelAndView ticket(HttpServletRequest request, HttpServletResponse response,Model model,RedirectAttributes attr,String ticket,String ip)
	{
		ModelAndView mav = new ModelAndView(FairyConstants.JSON_VIEW_RETAIN_DEFINED_VIEW); 
	    //ModelAndView mav = new ModelAndView("index"); 
	    //String ticket = request.getParameter("ticket");
        // String username = JVMCache.TICKET_AND_NAME.get(ticket);
        //JVMCache.TICKET_AND_NAME.remove(ticket);
		log.info("来自["+request.getContextPath()+"]的请求");
        Factory factory = new SSOCacheFactory(); 
        String cacheType=fairyConfig.getCacheType();
        SSOCache cache = factory.createSSOCache(cacheType);
        log.info("cacheType is:"+cacheType+",cache is:"+cache.getClass().getName());
           
 
        Ticket t=cache.get(ticket);
        

        log.info("ticket:"+ticket);
        String username=t.getAccountNo();
        //cache.delete(ticket);
 
        log.info("返回给前端数据数据:"+username);
        /**
        PrintWriter writer=null;
		try 
		{
			 writer = response.getWriter();
			 writer.write(username);
		} catch (IOException e) {

			e.printStackTrace();
		}
        */
	    mav.addObject(FairyConstants.JSON_VIEW_RETAIN_DATA,username);
	    return mav;
	}
	@RequestMapping ("/ip.do")
	@FairyControllerExceptionLog(description="通过Ip 或者域名获取ticket")
	public ModelAndView ip(HttpServletRequest request, HttpServletResponse response,Model model,RedirectAttributes attr,String ip)
	{
		ModelAndView mav = new ModelAndView(FairyConstants.JSON_VIEW_RETAIN_DEFINED_VIEW); 
	    //ModelAndView mav = new ModelAndView("index"); 
	    //String ticket = request.getParameter("ticket");
        // String username = JVMCache.TICKET_AND_NAME.get(ticket);
        //JVMCache.TICKET_AND_NAME.remove(ticket);
		log.info("来自["+request.getContextPath()+"]的请求");
        Factory factory = new SSOCacheFactory(); 
        String cacheType=fairyConfig.getCacheType();
        SSOCache cache = factory.createSSOCache(cacheType);
        log.info("cacheType is:"+cacheType+",cache is:"+cache.getClass().getName());
         
        String ticket="";
        
        String cilentUrl=fairyConfig.getSsoClientUrl();
        //ArrayList<String>ipList=SSOHelp.getIpList(cilentUrl);
        
        Ticket t=null;
        
    	if(cilentUrl.contains(ip))
    	{
            String clusterIp=fairyConfig.getSsoClientUrl();
            //String md5Ticket=getMD5Ticket(ticket);
            String ticketSecretkey=fairyConfig.getSsoTicketSecretkey();
            String md5Ticket=SSOHelp.getMD5Ticket(clusterIp, ticketSecretkey);
 
    		t=cache.get(md5Ticket);

    		log.info("ip  "+ip+"  置换       ticket is "+t.toString());
    	}

        //Ticket t=cache.get(ticket);
        

        log.info("ticket:"+ticket);
        String username=t.getAccountNo();
        //cache.delete(ticket);
 
        log.info("返回给前端数据数据:"+username);
        /**
        PrintWriter writer=null;
		try 
		{
			 writer = response.getWriter();
			 writer.write(username);
		} catch (IOException e) {

			e.printStackTrace();
		}
        */
	    mav.addObject(FairyConstants.JSON_VIEW_RETAIN_DATA,username);
	    return mav;
	}
}
